

//事件模块
var events = require('events')
var eventEmitter = new events.EventEmitter()

// 监听器1/2
var listen1 = function () {
    console.log(">>>>> listen1 call")
}
var listen2 = function () {
    console.log(">>>>> listen2 call")
}

//绑定事件 
eventEmitter.addListener("connection", listen1)
eventEmitter.on("connection", listen2)

var err = true  //false true
if (err) {
    eventEmitter.on("error", function (p) {
        console.log('error for some:', p)
    })  //错误处理 防止程序退出
}

var listenCnt = eventEmitter.listenerCount('connection')
console.log('listen count(1):', listenCnt)

//触发
eventEmitter.emit('connection')

//删除
eventEmitter.removeListener('connection', listen2)
console.log('remove listen1')

//再次触发
eventEmitter.emit('connection')


var listenCnt = eventEmitter.listenerCount('connection')
console.log('listen count(2):', listenCnt)

//宕机
eventEmitter.emit('error')

//输出堆栈
console.log('err stack at:', new Error().stack)

console.log('-------------------------------------- test event ok --------------------')


module.exports = {
    f0: function (a) {
        console.log('call f0:', a)
    },
    f1: function (a) {
        console.log('call f1:', a)
    }
}

/* module.exports.f1 = function(a) {
    console.log('call f1 --:', a)
} */

    

//测试接口
/* exports.o = {
    f1: function(a) {
       console.log('call f1:', a) 
    }
} */

